//
//  HMNewFeautreCell.swift
//  Weibo
//
//  Created by xiongminghua on 16/7/25.
//  Copyright © 2016年 熊明华. All rights reserved.
//

import UIKit

/// 新特性cell
class HMNewFeautreCell: UICollectionViewCell {
    
    
    // 设置一个属性，接收外面传的图片赋值
    var image: UIImage? {
        
        didSet {
            //给背景图片赋值
            bkgImageView.image = image
        }
    }
    
    // 通过代码创建，一定会走这个方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // // MARK: - 公开的方法
    func showButtons(isShow: Bool) {
        
        sharedButton.hidden = !isShow
        startButton.hidden = !isShow
    }
    
    
    // MARK: - 点击事件方法
    /// 点击分享按钮
    func sharedButtonClick() {
        
        self.sharedButton.selected = !sharedButton.selected
    }
    
    /// 点击开始按钮
    func startButtonClick() {
        // 切换界面到微博界面
        AppDelegate.swithRootViewController(HMTabBarController())
    }
    
    
    // MARK: - 私有方法
    
    /// 设置界面
    private func setupUI() {
        
        // 添加子控件
        self.contentView.addSubview(bkgImageView)
        self.contentView.addSubview(sharedButton)
        self.contentView.addSubview(startButton)
        
        // 添加约束
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
        }
        
        // 开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-160)
        }
        
        // 分享按钮
        sharedButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(startButton.snp_top).offset(-16)
        }

    }
    
    
    // MARK: - 懒加载
    /// 背景图片, 默认加载第一张图片
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 分享按钮
    private lazy var sharedButton: UIButton = {
        
        let button: UIButton = UIButton(type: UIButtonType.Custom)
        // 设置
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        button.setTitle(" 分享给大家", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 点击事件
        button.addTarget(self, action: "sharedButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button: UIButton = UIButton(type: UIButtonType.Custom)
        
        // 设置
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 点击事件
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    

}
